package com.baizhi.service.impl;

import com.baizhi.entity.Category;
import com.baizhi.entity.Menu;
import com.baizhi.mapper.CategoryMapper;
import com.baizhi.service.ICategoryService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 商品三级分类 服务实现类
 * </p>
 *
 * @author longyang
 * @since 2022-01-19
 */
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements ICategoryService {
    @Autowired
    private CategoryMapper categoryMapper;
    //根据父类别的编号查询该类别下面所有的子类别
    @Override
    public List<Category> selectCategorySon(Integer categoryId){
        //条件构造器：用于构建sql条件
        QueryWrapper<Category> queryWrapper = new QueryWrapper<>();
        //获取角色所有权限id且查询权限表内的对应id信息并返回
        queryWrapper.eq("parent_id",categoryId);
        return categoryMapper.selectList(queryWrapper);
    }
    //添加类别
    @Override
    public Integer addCategory(Category category){
        return categoryMapper.insert(category);
    }
    //删除类别
    @Override
    public Integer deleteCategory(Integer categoryId){
        return categoryMapper.deleteById(categoryId);
    }
    //根据id查询类别信息
    @Override
    public Category selectCategoryById(Integer categoryId){
        //条件构造器：用于构建sql条件
        QueryWrapper<Category> queryWrapper = new QueryWrapper<>();
        //获取角色所有权限id且查询权限表内的对应id信息并返回
        queryWrapper.eq("category_id",categoryId);
        return categoryMapper.selectOne(queryWrapper);
    }
    //修改类别
    @Override
    public Integer updateCategory(Category category){
        return categoryMapper.updateById(category);
    }
    //查询整个类别树
    @Override
    public List<Category> selectCategoryTree(){
        return categoryMapper.selectList(null);
    }
}
